package demo3;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class Text03 {
    public static void main(String[] args) throws Exception {
        // 初始化一个锁
        ReentrantLock lock = new ReentrantLock();

        try {
            // 开始执行业务代码之前先上锁
            lock.lock();
            System.out.println("业务代码执行中....");
            TimeUnit.SECONDS.sleep(3);
            throw new Exception("执行出现异常");
        } finally {
            // 无论任何时候都可以释放锁
            lock.unlock();
            System.out.println("锁已释放");
        }
    }
}
